<%@page import="com.glowdayz.glowmee.dao.ReviewCommentDAO"%>
<%@page import="com.glowdayz.glowmee.dao.EventCommentDAO"%>
<%@page import="com.glowdayz.glowmee.cmd.EventCommentCMD"%>
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@page import="com.glowdayz.common.DBHandler"%>
<%@page import="com.glowdayz.common.ApiHandler"%>
<%@page import="com.glowdayz.common.ApiHandler.XmlData"%>
<%@page import="java.util.ArrayList"%>
<%
	DBHandler.getInstance().connect();

	ApiHandler web = new ApiHandler(request, out, "JSPglowmee");
	
	int idRegister = web.getInt("idRegister");
	int idProduct = web.getInt("idProduct");
	String reviewText = web.getValue("reviewText");
	int rating = web.getInt("rating");
	int check = web.getInt("check");
	
	int insert = 0;
	
	int reviewCount = DBHandler.getInstance().getCount("reviewcomment", "*", "idRegister=" + idRegister + " AND idProduct=" + idProduct);
	
	if (reviewCount > 0) {
		DBHandler.getInstance().disconnect();
		web.result(null, "이미 리뷰를 작성한 제품입니다.");
		return;
	}
	
	if (check == 1) {
		DBHandler.getInstance().disconnect();
		web.result("SUCCESS", null);
		return;
	}
	
	insert = ReviewCommentDAO.getInstance().addReviewProduct(idRegister, idProduct, reviewText, rating);
	
	if (insert < 1) {
		DBHandler.getInstance().disconnect();
		web.result(null, "등록에 실패하였습니다.");
		return;
	}
	
	DBHandler.getInstance().disconnect();
	
	ArrayList<XmlData> list = new ArrayList<XmlData>();
	list.add(new XmlData("seq", insert));
	web.result("SUCCESS", "리뷰가 등록되었습니다.", list);
%>